package com.jxust.vhr.controller;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.jxust.debate.pojo.*;
import com.jxust.debate.util.CommonResult;
import com.jxust.debate.util.ResultEnum;
import com.jxust.vhr.service.CacodeService;
import com.jxust.vhr.service.CompanyAdminService;
import com.jxust.vhr.service.CompanyService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "公司管理员Controller",tags = "公司管理员操作接口")
@RestController
@RequestMapping("admin/CompanyAdmin")
public class CompanyAdminController {

    @Autowired
    private CacodeService cacodeService;

    @Autowired
    private CompanyAdminService companyAdminService;

    @Autowired
    private CompanyService companyService;

    /**
     * 公司管理员认证
     * @param companyAdministrator 公司管理员注册信息
     * @param cacode 认证码
     * @return 返回注册信息
     */
    @ApiOperation("公司管理员注册")
    @PostMapping("Register")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "caaccount",value = "公司管理员账号",required = true),
            @ApiImplicitParam(name = "capassword",value = "公司管理员密码",required = true),
            @ApiImplicitParam(name = "caname",value = "公司管理员名字",required = true),
            @ApiImplicitParam(name = "cacode",value = "公司管理员激活码,首两个字母为CA",required = true),
    })
    public CommonResult Register(CompanyAdministrator companyAdministrator,Cacode cacode){
        //查看传入的认证码是否有效
        CommonResult commonResult;
        commonResult= cacodeService.VerifyCode(cacode);
        if(commonResult.getCode()== ResultEnum.SUCCESS.getCode()){
            //查看此账号是否已被注册
           commonResult= companyAdminService.VerifyAccount(companyAdministrator.getCaaccount());
           if(commonResult.getCode()==ResultEnum.SUCCESS.getCode()){
               commonResult=companyAdminService.Register(companyAdministrator,cacode);
           }
        }
        return commonResult;
    }

    /**
     * 登录
     * @param companyAdministrator 账号与密码
     * @return 返回登录信息（密码置空)
     */
    @ApiOperation("公司管理员登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "caaccount", value = "公司管理员账号", required = true),
            @ApiImplicitParam(name = "capassword", value = "公司管理员密码", required = true)
    })
    @PostMapping("Login")
    public CommonResult<CompanyAdministrator> Login(CompanyAdministrator companyAdministrator){
        return companyAdminService.Login(companyAdministrator);
    }

    /**
     * 查询此管理员所属公司信息
     * @param caid 管理员ID
     * @return 返回公司信息
     */
    @ApiOperation("查询此管理员所属公司信息")
    @PostMapping("Affiliation")
    public CommonResult<Company> Affiliation(@RequestParam @ApiParam(name = "caid",value = "管理员ID",required = true) String caid){
        if(StrUtil.hasEmpty(caid)){
            CommonResult commonResult=new CommonResult();
            commonResult.setCode(ResultEnum.FAILURE.getCode());
            commonResult.setMessage("传入空值");
            return commonResult;
        }else
            return companyAdminService.Affiliation(caid);
    }

    /**
     * 创建公司部门
     * @param dname 创建的部门名字
     * @param cid 所属公司ID
     * @return
     */
    @ApiOperation("创建公司部门")
    @PostMapping("CreatDepartment")
    public CommonResult<Department> CreatDepartment(@RequestParam @ApiParam(name = "dname",value = "创建的部门名字",required = true) String dname,
                                                   @RequestParam @ApiParam(name = "cid",value = "所属公司ID",required = true) String cid){
        //验证公司ID是否正确
       CommonResult<Department> commonResult= companyService.VerifyCID(cid);
       if(commonResult.getCode()==ResultEnum.SUCCESS.getCode()){
           Department department=new Department();
           department.setDname(dname);
           department.setSubordinate(cid);
           department.setDid(RandomUtil.randomString(32));
           department.setDnumber(0);
           commonResult=companyAdminService.CreatDepartment(department);
           if(commonResult.getCode()==ResultEnum.SUCCESS.getCode()){
               commonResult.setData(department);
           }
       }
        return commonResult;
    }


    /**
     * 为部门管理员或HR创建认证码
     * @param cid 传入公司ID
     * @return 返回创建的认证码（返回的认证码首两字母为DM）
     */
    @ApiOperation("为部门管理员或HR创建认证码")
    @PostMapping("/CodeCreate")
    public CommonResult<Dmcode> CodeCreate(@RequestParam @ApiParam(name = "cid",value = "传入公司ID",required = true) String cid){
        return companyAdminService.CodeCreate(cid);
    }

    //return new CommonResult<>(101,"test meg");
}
